import { useModel, history } from "umi";
import { useReactive } from "ahooks";
import { useEffect } from "react";

const useWaitOnline = () => {
    const { remoteData } = useModel("global");
    const { remoteOrganizationInfo } = useModel("organization.info");

    const state = useReactive({
        loading: false,
        applications: [],
    });

    const loadWaitOnlineApplications = async () => {
        state.loading = true;
        let res = await remoteData({
            object: "bnt-application-resource-center-service",
            serviceName: "/developer/person/application/getWaitingOnline",
            params: {
                appSduu: remoteOrganizationInfo.uniformSocialCreditCode,
            },
        });
        state.applications = res?.filter((item) => item.ApplicationVersions.length > 0) || [];
        state.loading = false;
    };

    useEffect(() => {
        remoteOrganizationInfo.uniformSocialCreditCode && loadWaitOnlineApplications();
    }, [remoteOrganizationInfo]);

    return { ...state, loadWaitOnlineApplications };
};

export default useWaitOnline;
